from decimal import Decimal
from decimal import localcontext

# 执行精确的浮点数运算
def demo_1():
    a = Decimal('4.2')
    b = Decimal('2.1')
    print(a + b)
    print((a + b) == Decimal('6.3'))

# Decimal模块主要用于涉及到金融的领域
def demo_2():
    a = Decimal('1.3')
    b = Decimal('1.7')
    print(a / b)
    # 使用localcontext进行可控精度的运算
    with localcontext() as ctx:
        ctx.prec = 3
        print(a / b)
    with localcontext() as ctx:
        ctx.prec = 50
        print(a / b)

if __name__ == '__main__':
    # demo_1()
    demo_2()